home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6139 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.5 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: a pointer problem
  5. Date: 22 Feb 1996 11:16:19 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4gife3INN4rr@keats.ugrad.cs.ubc.ca>
  8. References: <4errk0$4c3@srvr1.engin.umich.edu> <31120AB7.1C44@cmt.lpr.mail.carel.fi> <4et96h$k84@sparcserver.lrz-muenchen.de> <4g1a9l$l5@mailhub.scitec.com.au>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4g1a9l$l5@mailhub.scitec.com.au>,
  12. Ramses Youhana <ramsesy@rd.scitec.com.au> wrote:
  13. >> I would and sometimes do count on initialization of static and global
  14. >> variables, as long as I work with an ANSI C compiler. Initialization
  15. >> happens "as if" through assigning 0 to the variable, so this kind of
  16. >> initialization is save for pointers and floating point variables, too.
  17. >
  18. >It is dangerous to rely on the compiler to do initialisation of static
  19. >and global variables in embedded systems.  This is usually the case when
  20.  
  21. It's not the compiler that does it. The whole environment of the program has to
  22. cooperate to guarantee the behavior. Linker, loader, etc. All the tools used in
  23. a standard conforming implementation have to work together to meet the
  24. standard. If any link in this chain of tools is broken, the implementation is
  25. non conforming.
  26.  
  27. >you write your own boot code and don't include the appropriate routine(s)
  28. >which read the data out of the appropriate section (sometimes called the
  29. >IDATA or INITDATA section) and copies to the appropriate RAM locations
  30. >to initialise your variables.
  31. >
  32. >I always find it better (and safer) design to perform my own initialisations
  33. >in an initialisation function.
  34.  
  35. The ANSI C standard requires that unitialized statics are zero, and it defines
  36. the behavior of global initializations.  Furthermore, this doesn't mean that
  37. you can just store a zero bit pattern in an unitialized data area. If you have
  38. any unitialized data types that have zero values that don't correspond to
  39. all-zero bit patterns, these have to be prepared analogously to initialized
  40. variables. If I leave a global pointer uninitialized, it will be a null pointer
  41. regardless of what bit pattern is used to represent it. Likewise for
  42. uninitialized floats.
  43.  
  44. If your environment does not set up these conditions for the C program,
  45. embedded or not, it is broken. If you write your own boot code, which launches
  46. the embedded C program, you have to make sure that it satisifies the weakest
  47. preconditions for running that program. If you want to deviate from the
  48. language standard, that is up to you, of course, but you will probably have a
  49. hard time integrating existing code that relies on the use of private static
  50. objects, since you will have to comb through every piece of code looking for
  51. static declarations, and including their initialization in a special routine.
  52. Of course, if any of these are private to functions, you will have to move them
  53. outside so that they are in the scope of the initializing function, and take
  54. care to resolve any name clashes...
  55.  
  56. ... isn't it easier to just write your boot code properly in the first place?
  57.  
  58.  
  59. You can't write programs that defend themselves against all possible broken
  60. compilers and environments. If you start initializing statics using explict
  61. code, where does the paranoia stop? Do you start using gotos everywhere just in
  62. case if(), while() and for() generate buggy code? What do you do if you don't
  63. trust that the environment will give you your string literals, or integer
  64. literal constants in statements like char *x = "foo" or int y = 3?  
  65. -- 
  66.  
  67.